import Varible from "../constants/index";

import { ResponseData } from "../types/common";
import { HttpMethods } from "../types/enum";

export default class RequestHttp {
  static get<T>(url: string, params?: object) {
    return request<T>(url, HttpMethods.GET, params)
  } 
  static post() {} 
  static put() {} 
  static delete() {} 
}
function request<T>(url: string, method: HttpMethods, data?: object) {
  return new Promise<T>((resolve, reject) => {
    wx.request<ResponseData<T>>({
      url: Varible.BASE_URL + url,
      method,
      data,
      success(res) {
        if (res.data.code === 200) {
          // 成功
          resolve(res.data.data)
        } else {
          wx.showToast({ 
            title: res.data.msg, 
            icon: 'error' 
          })
          reject(new Error(res.data.msg))
        }
      },
      fail(res) {
        reject(res.errMsg)
      }
    })
  })
}